package bb.lanxing.lib.devices.bici.cmd;

//import com.umeng.analytics.pro.cc;
import bb.lanxing.lib.devices.bici.BLECommand;
import bb.lanxing.lib.devices.utils.ByteUtils;
import java.nio.ByteBuffer;
import java.util.Map;

public class FirmwareCMD extends BLECommand {
    public static final byte COMMAND_ID = 1;
    public static final byte KEY_CONTENT = 5;
    public static final byte KEY_NAME = 1;
    public static final byte KEY_PACKAGE_COUNT = 3;
    public static final byte KEY_PACKAGE_INDEX = 4;
    public static final byte KEY_RESPONSE_INDEX = 6;
    public static final byte KEY_SIZE = 2;
    public static final int PER_PACKAGE_INTERVAL = 250;
    public static final int PER_PACKAGE_SIZE = 120;
    public static boolean SendAble = false;
    private byte[] content;
    private String fileName;
    private int fileSize;
    private short packageCount;
    private short packageIndex;
    private short responseIndex;

    public FirmwareCMD() {
        this.commandId = (byte) 1;
    }

    public FirmwareCMD(byte[] bArr) {
        super(bArr);
    }

    @Override
    public void parseBody(byte[] bArr) {
        if (bArr.length < 3) {
            return;
        }
        ByteBuffer wrap = ByteBuffer.wrap(bArr);
        byte b = wrap.get();
        short s = wrap.getShort();
        byte[] bArr2 = null;
        if (bArr.length > s - 3) {
            bArr2 = new byte[(bArr.length - s) - 3];
        }
        if (b == 6) {
            this.responseIndex = wrap.getShort();
        }
        if (bArr2 == null) {
            return;
        }
        wrap.get(bArr2);
        parseBody(bArr2);
    }

    public String toString() {
        return "fileName = " + this.fileName + "\n fileSize = " + this.fileSize + "\n packageCount = " + ((int) this.packageCount) + "\n packageIndex = " + ((int) this.packageIndex) + "\n content = " + ByteUtils.printlnByteArrayToUnsignHexString(this.content);
    }

    @Override
    protected byte[] encodeCMDBody(Map<Byte, Object> map) {
        int i;
        ByteBuffer allocate = ByteBuffer.allocate(500);
        allocate.put((byte) 1);
        allocate.put((byte) 16); //cc.n
        if (map.get((byte) 1) != null) {
            allocate.put((byte) 1);
            String str = (String) map.get((byte) 1);
            allocate.putShort((short) str.getBytes().length);
            allocate.put(str.getBytes());
            i = str.getBytes().length + 5;
        } else {
            i = 2;
        }
        if (map.get((byte) 2) != null) {
            allocate.put((byte) 2);
            int intValue = (Integer) map.get((byte) 2);
            allocate.putShort((short) 4);
            allocate.putInt(intValue);
            i += 7;
        }
        if (map.get((byte) 3) != null) {
            allocate.put((byte) 3);
            short shortValue = (Short) map.get((byte) 3);
            allocate.putShort((short) 2);
            allocate.putShort(shortValue);
            i += 5;
        }
        if (map.get((byte) 4) != null) {
            allocate.put((byte) 4);
            short shortValue2 = (Short) map.get((byte) 4);
            allocate.putShort((short) 2);
            allocate.putShort(shortValue2);
            i += 5;
        }
        if (map.get((byte) 5) != null) {
            allocate.put((byte) 5);
            byte[] bArr = (byte[]) map.get((byte) 5);
            allocate.putShort((short) bArr.length);
            allocate.put(bArr);
            i = i + 3 + bArr.length;
        }
        byte[] bArr2 = new byte[i];
        allocate.flip();
        allocate.get(bArr2);
        return bArr2;
    }

    public short getResponseIndex() {
        return this.responseIndex;
    }

    public void setResponseIndex(short s) {
        this.responseIndex = s;
    }
}
